package com.j2eefast.flowable.erp.controller;

import com.j2eefast.common.core.base.entity.LoginUserEntity;
import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.utils.*;
import com.j2eefast.flowable.bpm.entity.BpmTaskFromEntity;
import com.j2eefast.flowable.bpm.enums.CommentOSSType;
import com.j2eefast.flowable.bpm.service.BpmTaskFromService;
import com.j2eefast.flowable.bpm.utils.CommontOSSUtils;
import com.j2eefast.framework.annotation.RepeatSubmit;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.framework.utils.UserUtils;
import com.j2eefast.system.oss.entity.SysOssEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.ui.ModelMap;

import java.util.List;
import java.util.Map;
import java.util.Date;
import org.springframework.web.bind.annotation.*;
import com.j2eefast.flowable.erp.entity.bpmErpPersonLoanEntity;
import com.j2eefast.flowable.erp.service.bpmErpPersonLoanService;

/**
 * ERP人事借款申请页面控制器
 * @author yhli
 * @date 2020-07-21 17:45
 */
@Controller
@RequestMapping("/erp/loan")
public class bpmErpPersonLoanController extends BaseController{

    private String prefix = "modules/erp/loan";
    @Autowired
    @Lazy(value = true)
    private CommontOSSUtils commontOSSUtils;
    @Autowired
    private bpmErpPersonLoanService bpmErpPersonLoanService;
    /**
    * 必须注入实例关联表单服务
    */
    @Autowired
    private BpmTaskFromService bpmTaskFromService;


    @RequiresPermissions("erp:loan:view")
    @GetMapping()
    public String loan(){
        return prefix + "/loan";
    }
        
    @RequestMapping("/list")
    @RequiresPermissions("erp:loan:list")
    @ResponseBody
    public ResponseData list(@RequestParam Map<String, Object> params,bpmErpPersonLoanEntity bpmErpPersonLoanEntity) {
		PageUtil page = bpmErpPersonLoanService.findPage(params,bpmErpPersonLoanEntity);
		return success(page);
    }    
            
    @GetMapping("/add")
    public String add(ModelMap mmap){
         bpmErpPersonLoanEntity bpmErpPersonLoan = new bpmErpPersonLoanEntity();
          mmap.put("bpmErpPersonLoan", bpmErpPersonLoan);
        LoginUserEntity loginInfo=UserUtils.getUserInfo();
        mmap.put("user", loginInfo);
        String serNumber=OrderCodeFactory.generateJKAgain(loginInfo.getId());
        mmap.put("serNumber", serNumber);
        return prefix + "/add";
    }
        /**
         * 定义关联表单申请表单URL对应此处
         */
        @GetMapping("/add/{id}")
        public String add(@PathVariable("id") Long id, ModelMap mmp){
            //通过页面传入的表单ID查询表单关联信息
            BpmTaskFromEntity bpmTaskFrom = bpmTaskFromService.getTaskFromById(id);
            mmp.put("bpmTaskFrom", bpmTaskFrom);
            mmp.put("user", UserUtils.getUserInfo());
            return prefix + "/add";
        }

    /**
     * 新增
     */
    @RepeatSubmit
    @RequiresPermissions("erp:loan:add")
    @BussinessLog(title = "ERP人事借款申请", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData addbpmErpPersonLoan(@Validated bpmErpPersonLoanEntity bpmErpPersonLoan){
        Long userID=UserUtils.getUserId();
        //初始化数据时将数据设置为未删除
        bpmErpPersonLoan.setDeleteFlag("0");
        bpmErpPersonLoan.setApplyUserid(userID);
        bpmErpPersonLoan.setProcessStatus(1L);
        bpmErpPersonLoan.setCreateUserid(userID);
        bpmErpPersonLoan.setCreateTime(new Date());
        bpmErpPersonLoan.setAccountingPerson(userID);
        return bpmErpPersonLoanService.savebpmErpPersonLoan(bpmErpPersonLoan)? success(): error("新增失败!");
    }    
    
    /**
     * 修改
     */
    @RequiresPermissions("erp:loan:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap){
        bpmErpPersonLoanEntity bpmErpPersonLoan = bpmErpPersonLoanService.getbpmErpPersonLoanById(id);
        mmap.put("bpmErpPersonLoan", bpmErpPersonLoan);
        return prefix + "/edit";
    }
    /**
     * 审批1
     */
    @RequiresPermissions("erp:loan:view1")
    @GetMapping("/view1/{id}")
    public String view1(@PathVariable("id") String id, ModelMap mmap){
        bpmErpPersonLoanEntity bpmErpPersonLoan = bpmErpPersonLoanService.getbpmErpPersonLoanById(id);
        mmap.put("bpmErpPersonLoan", bpmErpPersonLoan);
        List<SysOssEntity> proof = commontOSSUtils.selectListByType(CommentOSSType.ERPLoanProof.getName(),bpmErpPersonLoan.getSerialNumber());
        mmap.put("proof",proof);
        List<SysOssEntity> proofList = commontOSSUtils.selectListByType(CommentOSSType.ERPLoanPaymentInfoProof.getName(),bpmErpPersonLoan.getSerialNumber());
        mmap.put("proofList",proofList);
        return prefix + "/view1";
    }
    /**
     * 审批2
     */
    @RequiresPermissions("erp:loan:view2")
    @GetMapping("/view2/{id}")
    public String view2(@PathVariable("id") String id, ModelMap mmap){
        bpmErpPersonLoanEntity bpmErpPersonLoan = bpmErpPersonLoanService.getbpmErpPersonLoanById(id);
        mmap.put("bpmErpPersonLoan", bpmErpPersonLoan);
        LoginUserEntity loginInfo=UserUtils.getUserInfo();
        mmap.put("user", loginInfo);
        List<SysOssEntity> proof = commontOSSUtils.selectListByType(CommentOSSType.ERPLoanProof.getName(),bpmErpPersonLoan.getSerialNumber());
        mmap.put("proof",proof);
        List<SysOssEntity> proofList = commontOSSUtils.selectListByType(CommentOSSType.ERPLoanPaymentInfoProof.getName(),bpmErpPersonLoan.getSerialNumber());
        mmap.put("proofList",proofList);
        return prefix + "/view2";
    }
    /**
     * 修改保存ERP人事借款申请
     */
    @RepeatSubmit
    @RequiresPermissions("erp:loan:view2")
    @BussinessLog(title = "ERP人事借款申请", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/view2", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData editbpmErpPersonLoanView2(bpmErpPersonLoanEntity bpmErpPersonLoan){
        bpmErpPersonLoan.setProcessStatus(2L);
        Long userID=UserUtils.getUserId();
        bpmErpPersonLoan.setUpdateUserid(userID);
        bpmErpPersonLoan.setAccountingPerson(userID);
        bpmErpPersonLoan.setAccountingPersonname(UserUtils.getUserInfo().getName());
        return bpmErpPersonLoanService.updatebpmErpPersonLoanById(bpmErpPersonLoan)? success(): error("修改失败!");
    }
    /**
     * 审批3
     */
    @RequiresPermissions("erp:loan:view3")
    @GetMapping("/view3/{id}")
    public String view3(@PathVariable("id") String id, ModelMap mmap){
        bpmErpPersonLoanEntity bpmErpPersonLoan = bpmErpPersonLoanService.getbpmErpPersonLoanById(id);
        mmap.put("bpmErpPersonLoan", bpmErpPersonLoan);
        List<SysOssEntity> proof = commontOSSUtils.selectListByType(CommentOSSType.ERPLoanProof.getName(),bpmErpPersonLoan.getSerialNumber());
        mmap.put("proof",proof);
        List<SysOssEntity> proofList = commontOSSUtils.selectListByType(CommentOSSType.ERPLoanPaymentInfoProof.getName(),bpmErpPersonLoan.getSerialNumber());
        mmap.put("proofList",proofList);
        return prefix + "/view3";
    }
    /**
     * 修改保存ERP人事借款申请
     */
    @RepeatSubmit
    @RequiresPermissions("erp:loan:view3")
    @BussinessLog(title = "ERP人事借款申请", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/view3", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData editbpmErpPersonLoanView3(bpmErpPersonLoanEntity bpmErpPersonLoan){
        bpmErpPersonLoan.setProcessStatus(3L);
        Long userID=UserUtils.getUserId();
        bpmErpPersonLoan.setUpdateUserid(userID);
        return bpmErpPersonLoanService.updatebpmErpPersonLoanById(bpmErpPersonLoan)? success(): error("修改失败!");
    }
    /**
     * 审批4
     */
    @RequiresPermissions("erp:loan:view4")
    @GetMapping("/view4/{id}")
    public String view4(@PathVariable("id") String id, ModelMap mmap){
        bpmErpPersonLoanEntity bpmErpPersonLoan = bpmErpPersonLoanService.getbpmErpPersonLoanById(id);
        mmap.put("bpmErpPersonLoan", bpmErpPersonLoan);
        List<SysOssEntity> proof = commontOSSUtils.selectListByType(CommentOSSType.ERPLoanProof.getName(),bpmErpPersonLoan.getSerialNumber());
        mmap.put("proof",proof);
        List<SysOssEntity> proofList = commontOSSUtils.selectListByType(CommentOSSType.ERPLoanPaymentInfoProof.getName(),bpmErpPersonLoan.getSerialNumber());
        mmap.put("proofList",proofList);
        return prefix + "/view4";
    }
    /**
     * 修改保存ERP人事借款申请
     */
    @RepeatSubmit
    @RequiresPermissions("erp:loan:view4")
    @BussinessLog(title = "ERP人事借款申请", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/view4", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData editbpmErpPersonLoanView4(bpmErpPersonLoanEntity bpmErpPersonLoan){
        bpmErpPersonLoan.setProcessStatus(4L);
        Long userID=UserUtils.getUserId();
        bpmErpPersonLoan.setUpdateUserid(userID);
        if(bpmErpPersonLoan.getPayConfirmpay()==null||!bpmErpPersonLoan.getPayConfirmpay().equals(1L)){
            bpmErpPersonLoan.setPayPaymentdate(null);
            bpmErpPersonLoan.setPayPreferencePaymethod(null);
            bpmErpPersonLoan.setPayMethodMemo(null);
            bpmErpPersonLoan.setPayProof(null);
            bpmErpPersonLoan.setPayRecipientName(null);
            bpmErpPersonLoan.setPayRecipientAddress(null);
            bpmErpPersonLoan.setPayRecipientPhone(null);
            bpmErpPersonLoan.setPayAccountNumber(null);
            bpmErpPersonLoan.setPayRoutingNumber(null);
            bpmErpPersonLoan.setPayBankAddress(null);
            bpmErpPersonLoan.setPayRmbRemittanceMethod(null);
            bpmErpPersonLoan.setPayBankName(null);
            bpmErpPersonLoan.setPayBankAccount(null);
            bpmErpPersonLoan.setPayAccountName(null);
        }
        else{
            if(bpmErpPersonLoan.getPayPreferencePaymethod()!=null&&bpmErpPersonLoan.getPayPreferencePaymethod().equals(1L)){
                bpmErpPersonLoan.setPayAccountNumber(null);
                bpmErpPersonLoan.setPayRoutingNumber(null);
                bpmErpPersonLoan.setPayBankAddress(null);
                bpmErpPersonLoan.setPayRmbRemittanceMethod(null);
                bpmErpPersonLoan.setPayBankName(null);
                bpmErpPersonLoan.setPayBankAccount(null);
                bpmErpPersonLoan.setPayAccountName(null);
            }else if(bpmErpPersonLoan.getPayPreferencePaymethod()!=null&&bpmErpPersonLoan.getPayPreferencePaymethod().equals(2L)){
                bpmErpPersonLoan.setPayRmbRemittanceMethod(null);
                bpmErpPersonLoan.setPayBankName(null);
                bpmErpPersonLoan.setPayBankAccount(null);
                bpmErpPersonLoan.setPayAccountName(null);
            }else if(bpmErpPersonLoan.getPayPreferencePaymethod()!=null&&bpmErpPersonLoan.getPayPreferencePaymethod().equals(3L)){
                bpmErpPersonLoan.setPayRecipientName(null);
                bpmErpPersonLoan.setPayRecipientAddress(null);
                bpmErpPersonLoan.setPayRecipientPhone(null);
                bpmErpPersonLoan.setPayAccountNumber(null);
                bpmErpPersonLoan.setPayRoutingNumber(null);
                bpmErpPersonLoan.setPayBankAddress(null);
            }
            else{
                bpmErpPersonLoan.setPayRecipientName(null);
                bpmErpPersonLoan.setPayRecipientAddress(null);
                bpmErpPersonLoan.setPayRecipientPhone(null);
                bpmErpPersonLoan.setPayAccountNumber(null);
                bpmErpPersonLoan.setPayRoutingNumber(null);
                bpmErpPersonLoan.setPayBankAddress(null);
                bpmErpPersonLoan.setPayRmbRemittanceMethod(null);
                bpmErpPersonLoan.setPayBankName(null);
                bpmErpPersonLoan.setPayBankAccount(null);
                bpmErpPersonLoan.setPayAccountName(null);
            }

        }
        return bpmErpPersonLoanService.updatebpmErpPersonLoanById(bpmErpPersonLoan)? success(): error("修改失败!");
    }
    /**
     * 审批5
     */
    @RequiresPermissions("erp:loan:view5")
    @GetMapping("/view5/{id}")
    public String view5(@PathVariable("id") String id, ModelMap mmap){
        bpmErpPersonLoanEntity bpmErpPersonLoan = bpmErpPersonLoanService.getbpmErpPersonLoanById(id);
        mmap.put("bpmErpPersonLoan", bpmErpPersonLoan);
        List<SysOssEntity> proof = commontOSSUtils.selectListByType(CommentOSSType.ERPLoanProof.getName(),bpmErpPersonLoan.getSerialNumber());
        mmap.put("proof",proof);
        List<SysOssEntity> proofList = commontOSSUtils.selectListByType(CommentOSSType.ERPLoanPaymentInfoProof.getName(),bpmErpPersonLoan.getSerialNumber());
        mmap.put("proofList",proofList);
        return prefix + "/view5";
    }
    /**
     * 修改保存ERP人事借款申请
     */
    @RepeatSubmit
    @RequiresPermissions("erp:loan:view5")
    @BussinessLog(title = "ERP人事借款申请", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/view5", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData editbpmErpPersonLoanView5(bpmErpPersonLoanEntity bpmErpPersonLoan){
        bpmErpPersonLoan.setProcessStatus(5L);
        Long userID=UserUtils.getUserId();
        bpmErpPersonLoan.setUpdateUserid(userID);
        bpmErpPersonLoan.setCashierSubmitTime(new Date());
        return bpmErpPersonLoanService.updatebpmErpPersonLoanById(bpmErpPersonLoan)? success(): error("修改失败!");
    }
    /**
     * 修改保存ERP人事借款申请
     */
    @RepeatSubmit
    @RequiresPermissions("erp:loan:edit")
    @BussinessLog(title = "ERP人事借款申请", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData editbpmErpPersonLoan(bpmErpPersonLoanEntity bpmErpPersonLoan){
        return bpmErpPersonLoanService.updatebpmErpPersonLoanById(bpmErpPersonLoan)? success(): error("修改失败!");
    }    

    /**
     * 删除
     */
    @RepeatSubmit
    @BussinessLog(title = "ERP人事借款申请", businessType = BusinessType.DELETE)
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @RequiresPermissions("erp:loan:del")
    @ResponseBody
    public ResponseData del(String[] ids) {
      return bpmErpPersonLoanService.deletebpmErpPersonLoanByIds(ids)? success(): error("删除失败!");
    }    
}
